package com.fsnbigdata.domain;

import com.fsnbigdata.util.ChineseInital;
import io.springlets.format.EntityFormat;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.Index;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import org.springframework.util.Assert;

/**
 * = EndangeredSpecies
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{name}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{name}")
public class EndangeredSpecies {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 物种名称
     */
    @NotNull
    @Size(min = 1, max = 255,message = "濒危物种名称长度必须1-255之间")
    private String name;

    /**
     * 物种拉丁名称
     */
    @Size(min = 0, max = 255,message = "濒危物种拉丁名称长度必须1-255之间")
    private String latin;

    /**
     * 拼音首字母
     */
    @Index(name="EndangeredSpecies_initial")
    private String initial;

    /**
     * 濒危状态
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String conservationStatus;

    /**
     * 分布
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String distribution;

    /**
     * 就地保护情况
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String protectionStatus;

    /**
     * 人为干扰强度
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String disturbances;

    /**
     * 生物id
     */
    @NumberFormat
    private Long organismId;

    /**
     * 创建者
     */
    @Size(min = 0, max = 255,message = "濒危物种创建者长度必须1-255之间")
    private String creator;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "endangeredSpecies", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    private Set<EndangeredSpeciesImage> images = new HashSet<EndangeredSpeciesImage>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof EndangeredSpecies)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((EndangeredSpecies) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getLatin() {
        return this.latin;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param latin
     */
    public void setLatin(String latin) {
        this.latin = latin;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getConservationStatus() {
        return this.conservationStatus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param conservationStatus
     */
    public void setConservationStatus(String conservationStatus) {
        this.conservationStatus = conservationStatus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getDistribution() {
        return this.distribution;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param distribution
     */
    public void setDistribution(String distribution) {
        this.distribution = distribution;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getProtectionStatus() {
        return this.protectionStatus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param protectionStatus
     */
    public void setProtectionStatus(String protectionStatus) {
        this.protectionStatus = protectionStatus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getDisturbances() {
        return this.disturbances;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param disturbances
     */
    public void setDisturbances(String disturbances) {
        this.disturbances = disturbances;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getOrganismId() {
        return this.organismId;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organismId
     */
    public void setOrganismId(Long organismId) {
        this.organismId = organismId;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<EndangeredSpeciesImage> getImages() {
        return this.images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param images
     */
    public void setImages(Set<EndangeredSpeciesImage> images) {
        this.images = images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String toString() {
        return "EndangeredSpecies {" + "id='" + id + '\'' + ", version='" + version + '\'' + ", name='" + name + '\'' + ", latin='" + latin + '\'' + ", initial='" + initial + '\'' + ", conservationStatus='" + conservationStatus + '\'' + ", distribution='" + distribution + '\'' + ", protectionStatus='" + protectionStatus + '\'' + ", disturbances='" + disturbances + '\'' + ", organismId='" + organismId + '\'' + ", creator='" + creator + '\'' + ", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' + ", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToAdd
     */
    public void addToImages(Iterable<EndangeredSpeciesImage> imagesToAdd) {
        Assert.notNull(imagesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (EndangeredSpeciesImage item : imagesToAdd) {
            this.images.add(item);
            item.setEndangeredSpecies(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToRemove
     */
    public void removeFromImages(Iterable<EndangeredSpeciesImage> imagesToRemove) {
        Assert.notNull(imagesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (EndangeredSpeciesImage item : imagesToRemove) {
            this.images.remove(item);
            item.setEndangeredSpecies(null);
        }
    }

    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
